'use strict';

const db = uniCloud.database()

const dbCmd = db.command

exports.main = async (event, context) => {
	
	const {
		userId,
		articleId
	} = event
	
	const user = await db.collection('user').doc(userId).get()
	
	const thumbs_up_article_ids = user.data[0].thumbs_up_article_ids
	
	let thumbs_ids = null 
	
	if(thumbs_up_article_ids.includes(articleId)) {
		return {
			code: 200,
			msg: '你已经点赞过该文章'
		}
	} else {
		thumbs_ids = dbCmd.addToSet(articleId)
	}
	
	await db.collection('user').doc(userId).update({
		thumbs_up_article_ids: thumbs_ids
	})
	
	await db.collection('user').doc(userId).update({
		thumbs_up_count: dbCmd.inc(1)
	})
	
	//返回数据给客户端
	return {
		code: 200,
		msg: '点赞成功'
	}
};
